package com.itheima.restkeeper.handler.wechat.operators.jspai;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.itheima.restkeeper.enums.TradingEnum;
import com.itheima.restkeeper.exception.ProjectException;
import com.itheima.restkeeper.handler.wechat.client.Config;
import com.itheima.restkeeper.handler.wechat.client.WechatPayHttpClient;
import com.itheima.restkeeper.handler.wechat.operators.baisc.SysytemParms;
import com.itheima.restkeeper.handler.wechat.response.JsapiPreCreateResponse;
import com.itheima.restkeeper.utils.ExceptionsUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * @ClassName JsapiPreCreate.java
 * @Description TODO
 */
@Data
@Log4j2
public class JsapiPreCreate  extends SysytemParms {

    public JsapiPreCreate(Config config) {
        super(config);
    }

    /***
     * @description Jsapi统一收单线下交易预创建
     *
     * @param outTradeNo  外部订单【业务系统中间的交易单号】
     * @param amount    交易的金额【单位：分】
     * @param description   商品的描述
     * @param openId    用户在当前公众号下的OpenID
     * @return: com.itheima.restkeeper.handler.wechat.response.JsapiPreCreateResponse
     */
    public JsapiPreCreateResponse jsapiPreCreate(String outTradeNo, String amount,
                                                 String description, String openId){
        //请求地址
        String uri ="/v3/pay/transactions/jsapi";
        //发起对应的请求
        WechatPayHttpClient httpClient = WechatPayHttpClient.builder()
                .mchId(mchId)
                .mchSerialNo(mchSerialNo)
                .apiV3Key(apiV3Key)
                .privateKey(privateKey)
                .domain(domain+uri)
                .build();
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode bodyParams = objectMapper.createObjectNode();
        BigDecimal bigDecimal = new BigDecimal(amount);
        BigDecimal multiply = bigDecimal.multiply(new BigDecimal(100));
        bodyParams.put("mchid",mchId)
                .put("appid",appid )
                .put("description", description)
                .put("notify_url", "https://www.weixin.qq.com/wxpay/pay.php")
                .put("out_trade_no", outTradeNo);
        //交易金额
        bodyParams.putObject("amount")
                .put("total", multiply.intValue());
        //付款者OpenId信息
        bodyParams.putObject("payer").put("openid",openId);
        String body = null;
        try {
            body =  httpClient.doPost(bodyParams);
        } catch (IOException e) {
            log.error("微信支付：preCreate，发生异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(TradingEnum.NATIVE_PAY_FAIL);
        }
        JsapiPreCreateResponse jsapiPreCreateResponse = JSONObject.parseObject(body, JsapiPreCreateResponse.class);
        jsapiPreCreateResponse.setCode("200");
        return jsapiPreCreateResponse;
    }
}
